using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Windows.Forms;

namespace Camalon.ColorPicker
{
    /// <summary>
    /// Summary description for ColorBox.
    /// </summary>
	internal class ColorBox:System.Windows.Forms.RadioButton
	{
		StringFormat _sf;
		private ColorItem _colorItem = new ColorItem(Color.FromArgb( 255,255,255,255), "Black");

		public ColorBox():this(new ColorItem())
		{
		
		}

		public ColorBox(ColorItem colorItem)
		{
			_colorItem = colorItem;
			_sf = StringFormat.GenericTypographic.Clone() as StringFormat;
			_sf.Alignment = _sf.LineAlignment = StringAlignment.Center;
			_colorItem = colorItem;
			this.Text = _colorItem.Name;

			this.SetStyle( ControlStyles.AllPaintingInWmPaint, true);
			this.SetStyle( ControlStyles.DoubleBuffer,true);
			this.SetStyle( ControlStyles.UserPaint, true );

			this.InitializeComponent();

			ColorRenderingValue.ValueChanged+=new EventHandler(ColorRenderingValue_ValueChanged);
			ColorRenderingValue.OpacityChanged+=new EventHandler(ColorRenderingValue_OpacityChanged);
			
			
			this.Invalidate();
		}



		private void InitializeComponent()
		{
			// 
			// ColorBox
			// 
			this.TabStop = true;

		}



		private void ColorRenderingValue_ValueChanged(object sender, EventArgs e)
		{
			if( this.Checked ) 
			{
				_colorItem.ColorRGB = ColorRenderingValue.Color;

				string text = ColorConverter.GetColorName(_colorItem.R,_colorItem.G, _colorItem.B, _colorItem.A);
				if( text == ColorConverter.UNKNOWN_COLOR_TEXT)
					text = ColorConverter.ARGBToHexString( _colorItem.Color );

				this.Text = text;

				Invalidate();
			}
		}


		private void ColorRenderingValue_OpacityChanged(object sender, EventArgs e)
		{
			if( this.Checked ) 
			{
				this._colorItem.A = ColorRenderingValue.Opacity;
				string text = ColorConverter.GetColorName(_colorItem.R,_colorItem.G, _colorItem.B, _colorItem.A);
				if( text == ColorConverter.UNKNOWN_COLOR_TEXT)
					text = ColorConverter.ARGBToHexString( _colorItem.Color );

				this.Text = text;

				this.Invalidate();
			}
		}


		protected override void OnCheckedChanged(EventArgs e)
		{
			if( this.Checked)
			{
				ColorRenderingValue.Color	=  _colorItem.ColorRGB;// this.BackColor;
				ColorRenderingValue.Opacity =  _colorItem.A;// _opacity;
				base.OnCheckedChanged (e);
			}
		}

		public override Color BackColor
		{
			get
			{
				return _colorItem.ColorRGB;
			}
			set
			{
				_colorItem.ColorRGB = Color.FromArgb( value.R, value.G, value.B);
				_colorItem.A = 255;
				
				base.BackColor = _colorItem.ColorRGB;

			}
		}


		public override string Text
		{
			get
			{
				return base.Text;
			}
			set
			{
				_colorItem.Name = value;
				base.Text = value;
			}
		}


		[Browsable( false)]
		[DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden)]
		public ColorItem ColorItem
		{
			get
			{
				return _colorItem;
			}
		}
		

//		private Font _font = new System.Drawing.Font("Comic Sans MS", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

		protected override void OnPaint(PaintEventArgs e)
		{
			Rectangle rect = this.ClientRectangle;
			SolidBrush brush = new SolidBrush( Color.White );
			e.Graphics.FillRectangle( brush, rect );

//			bool opacityEnabled = ColorRenderingValue.OpacityEnabled;
//			if( opacityEnabled && _colorItem.A < 255 )
//			{
//				e.Graphics.DrawString( "A", _font, Brushes.Gray, rect, _sf);
//			}
			

			Color penColor = this.Checked ? Color.Black:SystemColors.ControlDark;
			
			using( Pen pen = new Pen( penColor, 3f ))
			{

				
				Rectangle rectPen = this.ClientRectangle;
				rectPen.Width--; rectPen.Height--;
				e.Graphics.DrawRectangle( pen, rectPen );
			}
			
			rect.Inflate(-5,-5);
			brush.Color = _colorItem.ColorRGB;// opacityEnabled ? _colorItem.Color : _colorItem.ColorRGB;// this.BackColor;
			using( brush) e.Graphics.FillRectangle( brush, rect );
		}

		protected override void OnMouseDown(MouseEventArgs e)
		{
			if( e.Button == MouseButtons.Right) this.Checked = true;
			base.OnMouseDown (e);
		}

	}
}
